package com.itheima.wisdomnext.controller;

import com.itheima.wisdomnext.Handler.WebSocketHandler;
import com.itheima.wisdomnext.entity.Message;
import com.itheima.wisdomnext.entity.ReportReplyTemplate;
import com.itheima.wisdomnext.entity.Result.ResponseResult;
import com.itheima.wisdomnext.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 消息管理控制器
 */
@CrossOrigin // 允许跨域请求
@RestController
@RequestMapping("/api/messages")
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 获取指定用户的所有聊天记录
     * @param userId 用户ID
     * @return 聊天消息列表
     */
    @GetMapping("/all/{userId}")
    public ResponseResult<Map<String, Map<String, Object>>> getAllMessages(@PathVariable Integer userId) {
        Map<String, Map<String, Object>> messagesGrouped = messageService.getMessagesGroupedByUser(userId);
        return ResponseResult.success("获取用户所有聊天记录成功！", messagesGrouped);
    }

    /**
     * 获取指定管理员的所有聊天记录
     * @param adminId 管理员ID
     * @return 聊天消息列表
     */
    @GetMapping("/admin/all/{adminId}")
    public ResponseResult<Map<String, Map<String, Object>>> getAllMessagesByAdmin(@PathVariable Integer adminId) {
        Map<String, Map<String, Object>> messagesGrouped = messageService.getMessagesGroupedByAdmin(adminId);
        return ResponseResult.success("获取管理员所有聊天记录成功！", messagesGrouped);
    }


    /**
     * 发送消息（支持图片上传）
     * @param message 消息对象
     * @return 发送结果
     */
    @PostMapping("/send")
    public ResponseResult<Object> sendMessage(
            @RequestBody Message message
    ) {
        messageService.sendMessage(message);
        return ResponseResult.success("消息发送成功！", null);
    }

    /**
     * 标记消息为已读
     * @param messageId 消息ID
     * @return 操作结果
     */
    @PostMapping("/markAsRead/{messageId}")
    public ResponseResult<Object> markAsRead(@PathVariable Integer messageId) {
        messageService.markMessageAsRead(messageId);
        return ResponseResult.success("消息标记为已读成功！", null);
    }

    /**
     * 获取所有举报回复消息模板
     * @return 模板列表
     */
    @GetMapping("/reportReplyTemplates")
    public ResponseResult<List<ReportReplyTemplate>> getAllReportReplyTemplates() {
        List<ReportReplyTemplate> templates = messageService.getAllReportReplyTemplates();
        return ResponseResult.success("获取举报回复消息模板成功！", templates);
    }
}
